package org.example.medical.controller;

import org.example.medical.entity.CostRecord;
import org.example.medical.service.CostRecordService;
import org.example.medical.utils.ApiResult;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/costRecords")
public class CostRecordApiController {
    @Autowired
    private CostRecordService costRecordService;

    // 1. 分页查询接口
    @GetMapping
    public ApiResult<PageResult<CostRecord>> listCostRecords(
            @RequestParam(required = false) String patientName,
            @RequestParam(required = false) String visitType,
            @RequestParam(required = false) String department,
            @RequestParam(required = false) String paymentMethod,
            @RequestParam(required = false) String recordTime,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        List<CostRecord> records = costRecordService.getCostRecords(
                patientName,
                visitType,
                department,
                paymentMethod,
                recordTime,
                page,
                size
        );
        Long total = (long) records.size();
        return ApiResult.success(new PageResult<>(records, total, page, size));
    }

    // 2. 新增费用记录
    @PostMapping
    public ApiResult<?> addCostRecord(@RequestBody CostRecord costRecord) {
        costRecordService.addCostRecord(costRecord);
        return ApiResult.success(null, "Cost record added successfully");
    }

    // 3. 更新费用记录
    @PutMapping("/{costId}")
    public ApiResult<?> updateCostRecord(
            @PathVariable Long costId,
            @RequestBody CostRecord costRecord
    ) {
        costRecord.setCostId(costId);
        costRecordService.updateCostRecord(costRecord);
        return ApiResult.success(null, "Cost record updated successfully");
    }

    // 4. 软删除费用记录
    @DeleteMapping("/{costId}")
    public ApiResult<?> softDeleteCostRecord(@PathVariable Long costId) {
        costRecordService.softDeleteCostRecord(costId);
        return ApiResult.success(null, "Cost record deleted successfully");
    }

    // 5. 批量软删除费用记录
    @PostMapping("/batchDelete")
    public ApiResult<?> batchSoftDeleteCostRecords(@RequestBody List<Long> costIds) {
        costRecordService.batchSoftDeleteCostRecords(costIds);
        return ApiResult.success(null, "Cost records deleted successfully");
    }
}